<?php

// No direct access to this file
defined('_JEXEC') or die('Restricted access');
JTable::addIncludePath(JPATH_COMPONENT.DIRECTORY_SEPARATOR.'tables');

class KetnoiModelEngine extends JModelLegacy
{
	public function getTable($type = 'Engine', $prefix = 'KetnoiTable', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}
	
	function loadGridData($limitstart,$limit,$where)
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("e.*,d.ten as tendonvi");
        $query->from("#__products e");
        $query->leftJoin("#__donvi d on d.id = e.id_donvi");
        $query->where($where);
        $query->where("e.product_type = 'mayphoto'");
        $query .= " limit $limitstart,$limit";  
        $db->setQuery($query);
        $result = $db->loadObjectList();
        return $result; 
    }
    
	function loadAllGridData($where)
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("count(*)");
        $query->from("#__products e");
        $query->leftJoin("#__donvi d on d.id = e.id_donvi");
        $query->where($where); 
        $query->where("e.product_type = 'mayphoto'");
        $db->setQuery($query);
        $result = $db->loadResult();
        return $result; 
    }
    
	function save()
    {
    	global $app;
    	$result = array("success"=>true,"error"=>"");
    	
    	$arr_upload = Utility::uploadImages("file");
    	if(!$arr_upload["success"])
    	{
    		return $arr_upload;
    	}else 
    	{
    		$old_file = JRequest::getVar("old_file","");
    		if(!empty($old_file))
    		{
    			$old_file = json_decode($old_file,true);
    			$delete_file = json_decode(JRequest::getVar("delete_file"),true);
    			$img_dir = JPATH_SITE.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'fileupload'.DIRECTORY_SEPARATOR;
    			
    			foreach ($old_file as $file)
    			{
    				if(!in_array($file, $delete_file))
    				{
    					$arr_upload["data"][] = $file;
    				}else 
    				{
    					if(JFile::exists($img_dir.$file))
    					{
    						JFile::delete($img_dir.$file);
    					}
    				}
    			}
    		}
    		
    		$images = json_encode($arr_upload["data"]);
    	
    		$table = $this->getTable();
	    	$post = JRequest::get("post");
	    	$post["product_type"] = "mayphoto";
	    	$post["hinhanh"] = $images;
			$table = Utility::save($table, $post);
			$result["id"] = $table->id;
			$result["ma"] = $table->ma;
			$result["ten"] = $table->ten;
			Utility::buildCacheMayPhoto();
    	}
    	return $result;
    }
    
    function save_engine_linhkien()
    {
    	$db = JFactory::getDbo();
    	$result = array("success"=>true,"error"=>"");
    	$post = JRequest::get("post");
    	$engine_id = $post["engine_id"];
		$linhkien_id = $post["linhkien_id"];
		if(empty($linhkien_id)) 
		{
			$result["success"] = false;
			$result["error"] = "Lưu thất bại";
			return $result;
		}
		$query = "select count(*) from #__engine_linhkien where engine_id = $engine_id and linhkien_id = $linhkien_id";
		$db->setQuery($query);
		if($db->loadResult() == 0)
		{		
			$query = "insert into #__engine_linhkien(engine_id,linhkien_id) values($engine_id,$linhkien_id)";
			$db->setQuery($query);
			if(!$db->query())
			{
				$result["success"] = false;
				$result["error"] = "Lưu thất bại";
			}
		}
		return $result;
    }
    
    function loadData($id)
    {
    	$table = $this->getTable();
    	$table->load($id);
    	return $table;
    }
    
	function checkMaExists()
    {
    	$ma = JRequest::getVar("ma");
    	$oldval = JRequest::getVar("oldval","");
    	if(strtolower($ma) == strtolower($oldval))
    	{
    		return false;
    	}
    	$db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("count(*)");
        $query->from("#__products");
        $query->where("is_delete = 0 and ma = '$ma' and product_type = 'mayphoto'");
        $db->setQuery($query);
        $result = $db->loadResult();
        return $result > 0;
    }
    
	function checkTenExists()
    {
    	$ten = JRequest::getVar("ten");
    	$oldval = JRequest::getVar("oldval","");
    	if(strtolower($ten) == strtolower($oldval))
    	{
    		return false;
    	}
    	$db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("count(*)");
        $query->from("#__products");
        $query->where("is_delete = 0 and ten = '$ten' and product_type = 'mayphoto'");
        $db->setQuery($query);
        $result = $db->loadResult();
        return $result > 0;
    }
    
	function delete()
	{
		$id = JRequest::getVar("id");
		$db = JFactory::getDbo();
		$query = "update #__products set is_delete = 1 where id = $id";
		$db->setQuery($query);
		$db->query();
		Utility::buildCacheMayPhoto();
	}
	
	function loadAllVendor()
	{
		$db = JFactory::getDbo();
		$query = "select * from #__vendor where is_delete = 0";
		$db->setQuery($query);
		return $db->loadObjectList();
	}
}
